package org.project.s.controller;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.project.pojo.SysUser;
import org.project.service.user.SysUserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;


@Controller
@RequestMapping("/logincontroller")
public class LoginController {
	@Resource
	private SysUserService sysUserService;
	@RequestMapping("/logindo")
	public String login(SysUser User){
		SysUser u = sysUserService.loginselect(User);
	
		if(u!=null){
            if(u.getLoginTime()!=null){
				u.setLastLoginTime(u.getLoginTime());
			}
			Date date = new Date();
			SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String logintime = sim.format(date);
			u.setLoginTime(logintime);
			sysUserService.loginupdate(u);
			
			 
			HttpServletRequest req = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();			
			HttpSession session=req.getSession();
			session.setAttribute("userid", u.getId());			
			session.setAttribute("username", u.getUserName());
			session.setAttribute("logintime", u.getLastLoginTime());
			session.setAttribute("role", u.getRole());
			return  "main1";
		}else{
			return "index";
		}
		
	}
	
	@RequestMapping("/exitlogin")
	public String exitlogin(HttpSession session){
		HttpServletRequest req = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();			
        session= req.getSession();
		session.invalidate();
		
		return  "index";
		
	}
	
	
	
	}
	

